<?php
/**
 * Action that logs in a user		Registed as 'action/user/login'
 *  
 * $email
 * $pass
 */

#################################################################
#								ACTION
#################################################################
if($_CONTEXT==CONTEXT_ACTION){

	$email= blg_get_input('email');
	$password= blg_get_input('pass');
	
	if(blg_email_exists($email)){
		$user= blg_get_user_entity_by_email($email);
		
		//NOT ACTIVE
		if($user->is_not_active()){
			blg_user_send_validation_email($user->guid);
			blg_msg_add(blg_echo('register:confirm1', array($user->email)), true);			
			blg_msg_add(blg_echo('register:confirm2', array($user->email)), true);				
			forward();			
		}
		
		//DO LOGIN
		if(blg_user_do_login($email, $password)){
			//LOGIN SUCCESFULL
			$user= blg_get_logged_in_user_entity();
			blg_msg_add(blg_echo('login:success', array($user->name)));
			forward();
		}
	}
	
	//LOGIN FAILED
	
	//Save form data
	blg_session_set('show_login_form', array('email' => $email,'pass' => $password), TRUE);
	
	//Try again
	blg_msg_add(blg_echo('login:failed'), true);
	forward(blg_get_site_url());
	
}
#################################################################
#								ACCESS
#################################################################
elseif($_CONTEXT==CONTEXT_ACCESS){ 
	$_ACCESS= (blg_user_is_logged_in()===FALSE && blg_security_grant_from_input(60*60));
}


